<?php

function get_support_detail($id) {
    include(dirname(__FILE__) . '/../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM supports where id=" . $id;

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }
    $is_login = 0;
    $contact = array();
    while ($row = mysqli_fetch_array($result)) {
        $contact = $row;
    }
    return $contact;
    mysqli_close($con);
}

function get_all_supports_of_user($user_id) {

    include(dirname(__FILE__) . '/../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
    // Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $user_id = mysql_real_escape_string($user_id);
    $sql = "SELECT * FROM supports where status!=3 and assigned_to=" . $user_id;
    $sql.=' ORDER BY date_created DESC';

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $contacts = array();
    while ($row = mysqli_fetch_array($result)) {
        $contacts[] = $row;
    }
    mysqli_close($con);
    return $contacts;
}

function get_all_comments($support_id = null) {
    require('../config.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM support_comment where 1";

    if ($support_id != null) {
        $support_id = mysql_real_escape_string($support_id);
        $sql.=' AND support_id = ' . $support_id . '';
    }
    $sql.=' ORDER BY date_created DESC';

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $contacts = array();
    while ($row = mysqli_fetch_array($result)) {
        $contacts[] = $row;
    }
    mysqli_close($con);
    return $contacts;
}

function get_all_supports($assigned_to = null) {
    require('../config.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM supports where 1";

    if ($assigned_to != null) {
        $assigned_to = mysql_real_escape_string($assigned_to);
        $sql.=' AND assigned_to = ' . $assigned_to . '';
    }
    $sql.=' ORDER BY date_created DESC';

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }

    $contacts = array();
    while ($row = mysqli_fetch_array($result)) {
        $contacts[] = $row;
    }
    mysqli_close($con);
    return $contacts;
}

function add_support_comment($post) {
    require('../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);

// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $keys = array();
    $values = array();
    $post['date_created'] = time();
    $post['user_comment'] = $_SESSION['id'];

    foreach ($post as $column_name => $value) {
        if ($column_name != 'go'&&$column_name!='id'&&$column_name!='status') {
            $value = trim($value);
            $value = mysql_real_escape_string($value);
            $keys[] = "`{$column_name}`";
            $value = mysql_real_escape_string($value);
            $values[] = "'{$value}'";
        }
    }
    $query = "INSERT INTO support_comment (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ");";
    //echo $query;
    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $query);
    if (!$result) {
        die(mysqli_error($con) . ':' . $query);
    }
    mysqli_close($con);
}

function add_support($post) {
    require('../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);

// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $keys = array();
    $values = array();
    $post['date_created'] = time();
    $post['creator'] = $_SESSION['id'];

    foreach ($post as $column_name => $value) {
        if ($column_name != 'go') {
            $value = trim($value);
            $value = mysql_real_escape_string($value);
            $keys[] = "`{$column_name}`";
            $value = mysql_real_escape_string($value);
            $values[] = "'{$value}'";
        }
    }
    $query = "INSERT INTO supports (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ");";
    //echo $query;
    mysqli_query($con, "SET NAMES 'utf8'");
    mysqli_query($con, $query);
    mysqli_close($con);
}

function update_support($post) {
    require('../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);

// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $keys = array();
    $values = array();
    $query = 'Update supports SET ';
    foreach ($post as $column_name => $value) {
        if ($column_name != 'go') {
            $value = trim($value);
            $value = mysql_real_escape_string($value);
            $query.=" `{$column_name}` = " . " '{$value}',";
        }
    }
    $query.="`id`=" . $post['id'];
    $query.=" Where id =" . $post['id'];


    mysqli_query($con, "SET NAMES 'utf8'");
    
    $result = mysqli_query($con, $query);
    if (!$result) {
        die(mysqli_error($con) . ':' . $query);
    }
    
    mysqli_close($con);
}

function delete_support($id) {
    require('../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);

    // Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $id = mysql_real_escape_string($id);
    $query = 'DELETE FROM supports WHERE id = ' . $id;
    mysqli_query($con, "SET NAMES 'utf8'");
    mysqli_query($con, $query);
    mysqli_close($con);
}

?>